package cn.yy.cloudpay.usb;

import android.content.Context;

import com.android.usbport.ModbusTransfer;
import com.android.usbport.USBDevice;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import cn.yy.cloudpay.scan.FeixiScanTActivity;
import cn.yy.cloudpay.util.SerialUtils;
import cn.yy.cloudpay.util.StringUtil;
import cn.yy.cloudpay.view.ToastUtil;

/**
 * Created by Administrator on 2018/1/27.
 */

public class ComMUtil {
    public Logger log= Logger.getLogger(this.getClass());
    private Context mContext;
    private FeixiScanTActivity activity;
    private int l=3;
    private int x = 3;
    private String redaddr = "0004";
    public static int write_1 = 414; //开机
    public static int write_2 = 416;//重启
    List<USBDevice> openDevices = new ArrayList<USBDevice>();

    public static ComMUtil instance(Context context){
        ComMUtil comm = new ComMUtil(context);
        comm.l = 3;
        comm.x = 3;
        return comm;
    }

    public static ComMUtil instance(Context context, Set<USBDevice> openDevices){
        ComMUtil comm = new ComMUtil(context);
        comm.l = 3;
        comm.x = 3;
        return comm;
    }

    private ComMUtil(Context context){
        this.mContext = context;
        this.activity = (FeixiScanTActivity)mContext;
    }

    private ComMUtil(Context context, Set<USBDevice> openDevices){
        this.mContext = context;
        this.activity = (FeixiScanTActivity)mContext;
        this.openDevices.addAll(openDevices);
    }

    //发送启动设备指令
    public void startup(int key){
        String adr = SerialUtils.readAddr(key);
        String data = SerialUtils.readData(1);
        byte[] ret = ModbusTransfer.getInstance(mContext).sendCmdToPlc(openDevices.get(0),
                Integer.parseInt("1"),
                StringUtil.hexStr2Bytes(adr),
                StringUtil.hexStr2Bytes(data));
        String result = ret==null? "指令发送失败！" : "指令发送成功："+ StringUtil.byte2HexStr(ret);
        if(ret==null&&l>=0){
            l--;
            log.info(redaddr+":指令发送失败 继续发送"+l);
            ToastUtil.showToast(mContext, ":指令发送失败 继续发送"+l, ToastUtil.FAIL);
            startup(key);
        }else{
            log.info(redaddr+":指令发送成功:"+result);

        }
    }

    //检测是否可以支付
    public void checkPay(){
        final byte[] ret = ModbusTransfer.getInstance(mContext).readDataFromPlc(openDevices.get(openDevices.size()),
                Integer.parseInt("1"),
                StringUtil.hexStr2Bytes(redaddr.trim()),
                Integer.parseInt("1"),
                3000);
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(ret != null) {
                    String rs = StringUtil.byte2HexStr(ret);
                    int equiment = Integer.valueOf(rs);
                    if(equiment==1&&redaddr.equals("0004")){
                        log.info(redaddr+":待机中 打开扫描支付");
                        activity.sendPay();
                    }else{
                        log.info(redaddr+":设备正在使用中，请稍候再支付");
                    }
                }else{
                    log.info(redaddr+":读取数据失败 重新检测是否可以支付");
                    l--;
                    if(l>=0){
                        log.info(redaddr+":再次检测是否可以支付 "+l);
                        checkPay();
                    }else{
                        log.info(redaddr+":检测完成");
                        ToastUtil.showToast(mContext, "检测是否可以支付，读取数据失败", ToastUtil.FAIL);
                    }
                }
            }
        });
    }

    //检测是否启动成功
    public void checkStart(){
        final byte[] ret = ModbusTransfer.getInstance(mContext).readDataFromPlc(openDevices.get(openDevices.size()),
                Integer.parseInt("1"),
                StringUtil.hexStr2Bytes(redaddr.trim()),
                Integer.parseInt("1"),
                3000);
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(ret != null) {
                    String rs = StringUtil.byte2HexStr(ret);
                    int equiment = Integer.valueOf(rs);
                    if(equiment==1&&redaddr.equals("0004")){
                        log.info(redaddr+":启动失败 再次发送指令");
                        x--;
                        if(x>=0) {
                            startup(write_1);
                        }
                    }else{
                        log.info(redaddr+":启动成功，正在运行。");
                    }
                }else{
                    log.info(redaddr+":读取数据失败 重新检测是否启动成功");
                    l--;
                    if(l>=0){
                        log.info(redaddr+":再次检测是否启动成功 "+l);
                        checkStart();
                    }else{
                        log.info(redaddr+":检测完成");
                        ToastUtil.showToast(mContext, "检测是否启动成功，读取数据失败", ToastUtil.FAIL);
                    }
                }
            }
        });
    }

    //检测是否重启设备
    public void checkReStart(){
        final byte[] ret = ModbusTransfer.getInstance(mContext).readDataFromPlc(openDevices.get(openDevices.size()),
                Integer.parseInt("1"),
                StringUtil.hexStr2Bytes(redaddr.trim()),
                Integer.parseInt("1"),
                3000);
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(ret != null) {
                    String rs = StringUtil.byte2HexStr(ret);
                    int equiment = Integer.valueOf(rs);
                    if(equiment==1&&redaddr.equals("0004")){
                        log.info(redaddr+":启动失败 重启设备");
                        startup(write_2);
                    }else{
                        log.info(redaddr+":启动成功，正在运行。");
                    }
                }else{
                    log.info(redaddr+":读取数据失败 重新检测是否重启设备");
                    l--;
                    if(l>=0){
                        log.info(redaddr+":再次检测是否重启设备 "+l);
                        checkReStart();
                    }else{
                        log.info(redaddr+":检测完成");
                        ToastUtil.showToast(mContext, "检测是否重启设备，读取数据失败", ToastUtil.FAIL);
                    }
                }
            }
        });
    }

    //最后检测是否重启设备
    public void lastCheck(){
        final byte[] ret = ModbusTransfer.getInstance(mContext).readDataFromPlc(openDevices.get(openDevices.size()),
                Integer.parseInt("1"),
                StringUtil.hexStr2Bytes(redaddr.trim()),
                Integer.parseInt("1"),
                3000);
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(ret != null) {
                    String rs = StringUtil.byte2HexStr(ret);
                    int equiment = Integer.valueOf(rs);
                    if(equiment==1&&redaddr.equals("0004")){
                        log.info(redaddr+":最后检测启动失败");
                        ToastUtil.showToast(mContext, "设备启动失败,请联系管理员", ToastUtil.FAIL);
                    }
                }else{
                    log.info(redaddr+":读取数据失败 最后检测检测是否重启设备");
                    l--;
                    if(l>=0){
                        log.info(redaddr+":最后检测是否重启设备 "+l);
                        lastCheck();
                    }else{
                        log.info(redaddr+":检测完成");
                        ToastUtil.showToast(mContext, "最后检测是否重启设备，读取数据失败", ToastUtil.FAIL);
                    }
                }
            }
        });
    }
}
